package sy.util.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * 文件名称 ：ClientUtil.java<br>
 * 内容摘要 ：<br>
 * 创建人　 ：xuhuaqiang<br>
 * 创建日期 ：2016年01月21日<br>
 * 版本号　 ：v1.0.0<br>
 * 公司　　 ：重庆第六边界<br>
 * 版权所有 ：(C)2001-2016<br>
 * 修改记录 ：<br>
 * 修改日期 ：<br>
 * 版本号 　：<br>
 * 修改人 　：<br>
 * 修改内容 ：<br>
 */
public class ClientUtil {

    public static String sendGet(String url, Map<String, Object> map) {
        return sendGet(url + "?" + paramMap(map));
    }
    public static String sendGet(String url) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.connect();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }

    public static String sendPost(String url, Map<String, Object> map) {
        return sendPost(url, paramMap(map));
    }

    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
                if (in != null) in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    private static String paramMap(Map<String, Object> map) {
        StringBuilder buffer = new StringBuilder();
        for (String key : map.keySet()) {
            buffer.append(key).append("=").append(map.get(key).toString()).append('&');
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }
}
